package com.fhyc.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.jeffreyning.mybatisplus.anno.MppMultiId;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Date;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * (LfForecast96lc)实体类
 *
 * @author makejava
 * @since 2025-05-18 15:48:25
 */
@Data
@TableName("so_in_lf_forecast_96lc")
public class SoInLfForecast96lc implements Serializable {
    private static final long serialVersionUID = 594211448895601384L;
    
    private Integer busid;
    
    private Date sjrq;
    
    private Integer ymd;
    
    private Double t0000;
    
    private Double t0015;
    
    private Double t0030;
    
    private Double t0045;
    
    private Double t0100;
    
    private Double t0115;
    
    private Double t0130;
    
    private Double t0145;
    
    private Double t0200;
    
    private Double t0215;
    
    private Double t0230;
    
    private Double t0245;
    
    private Double t0300;
    
    private Double t0315;
    
    private Double t0330;
    
    private Double t0345;
    
    private Double t0400;
    
    private Double t0415;
    
    private Double t0430;
    
    private Double t0445;
    
    private Double t0500;
    
    private Double t0515;
    
    private Double t0530;
    
    private Double t0545;
    
    private Double t0600;
    
    private Double t0615;
    
    private Double t0630;
    
    private Double t0645;
    
    private Double t0700;
    
    private Double t0715;
    
    private Double t0730;
    
    private Double t0745;
    
    private Double t0800;
    
    private Double t0815;
    
    private Double t0830;
    
    private Double t0845;
    
    private Double t0900;
    
    private Double t0915;
    
    private Double t0930;
    
    private Double t0945;
    
    private Double t1000;
    
    private Double t1015;
    
    private Double t1030;
    
    private Double t1045;
    
    private Double t1100;
    
    private Double t1115;
    
    private Double t1130;
    
    private Double t1145;
    
    private Double t1200;
    
    private Double t1215;
    
    private Double t1230;
    
    private Double t1245;
    
    private Double t1300;
    
    private Double t1315;
    
    private Double t1330;
    
    private Double t1345;
    
    private Double t1400;
    
    private Double t1415;
    
    private Double t1430;
    
    private Double t1445;
    
    private Double t1500;
    
    private Double t1515;
    
    private Double t1530;
    
    private Double t1545;
    
    private Double t1600;
    
    private Double t1615;
    
    private Double t1630;
    
    private Double t1645;
    
    private Double t1700;
    
    private Double t1715;
    
    private Double t1730;
    
    private Double t1745;
    
    private Double t1800;
    
    private Double t1815;
    
    private Double t1830;
    
    private Double t1845;
    
    private Double t1900;
    
    private Double t1915;
    
    private Double t1930;
    
    private Double t1945;
    
    private Double t2000;
    
    private Double t2015;
    
    private Double t2030;
    
    private Double t2045;
    
    private Double t2100;
    
    private Double t2115;
    
    private Double t2130;
    
    private Double t2145;
    
    private Double t2200;
    
    private Double t2215;
    
    private Double t2230;
    
    private Double t2245;
    
    private Double t2300;
    
    private Double t2315;
    
    private Double t2330;
    
    private Double t2345;
    
    private Double t2400;

    /**
     * 数据类型：1实况值，2预测值，中调上报预测值
     */
    @MppMultiId
    @JsonIgnore
    @TableField(exist = false)
    private Integer dataType;


    @JsonIgnore
    @TableField(exist = false)
    private String busName;

    @TableField(exist = false)
    private List<Double> timePoints; // 存储 96 个时间点的值


    public void SoInBusLoadInfoEntity(Integer busId, Integer dataType, Integer ymd, List<Double> valueList) {
        this.busid = busId;
        this.dataType = dataType;
        this.ymd = ymd;

        if (valueList.size() != 96) {
            throw new IllegalArgumentException("valueList must contain 96 values.");
        }

        IntStream.range(0, 96).forEach(i -> {
            int hour = i / 4;
            int minute = (i % 4) * 15;
            String fieldName = String.format("t%02d%02d", hour, minute);
            try {
                this.getClass().getDeclaredField(fieldName).set(this, valueList.get(i));
            } catch (IllegalAccessException | NoSuchFieldException e) {
                throw new RuntimeException(e);
            }
        });
    }

    /**
     * 获取全天96点数据
     *
     * @return 全天96点数据
     */
    public Double[] getValues() {
        return new Double[]{t0000, t0015, t0030, t0045, t0100, t0115, t0130, t0145, t0200, t0215, t0230, t0245, t0300, t0315, t0330, t0345, t0400, t0415, t0430, t0445, t0500, t0515, t0530, t0545, t0600, t0615, t0630, t0645, t0700, t0715, t0730, t0745, t0800, t0815, t0830, t0845, t0900, t0915, t0930, t0945, t1000, t1015, t1030, t1045, t1100, t1115, t1130, t1145, t1200, t1215, t1230, t1245, t1300, t1315, t1330, t1345, t1400, t1415, t1430, t1445, t1500, t1515, t1530, t1545, t1600, t1615, t1630, t1645, t1700, t1715, t1730, t1745, t1800, t1815, t1830, t1845, t1900, t1915, t1930, t1945, t2000, t2015, t2030, t2045, t2100, t2115, t2130, t2145, t2200, t2215, t2230, t2245, t2300, t2315, t2330, t2345};
    }

    /**
     * 获取全天96点数据列表
     *
     * @return 全天96点数据列表
     */
    @JsonIgnore
    public List<Double> getValueList() {
        return Arrays.asList(getValues()).stream().map(value -> value != null ? new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue() : null).collect(Collectors.toList());
    }

    /**
     * 获取指定索引之间（前闭后闭）的值
     *
     * @param start 起始索引，00:00 的索引为 0
     * @param end   结束索引，23:45 的索引为 95
     * @return 指定索引之间（前闭后闭）的值
     */
    @JsonIgnore
    public Double[] getValues(int start, int end) {
        int subArrayLength = end - start + 1;
        Double[] subArray = new Double[subArrayLength];
        System.arraycopy(getValues(), start, subArray, 0, subArrayLength);
        return subArray;
    }

    /**
     * 获取指定索引之间（前闭后闭）的值
     *
     * @param start 起始索引，00:00 的索引为 0
     * @param end   结束索引，23:45 的索引为 95
     * @return 指定索引之间（前闭后闭）的值
     */
    @JsonIgnore
    public List<Double> getValueList(int start, int end) {
        return Arrays.asList(getValues()).subList(start, end);
    }


    /**
     * 获取指定时间区间（前闭后闭）的值
     *
     * @param start，如：00:00
     * @param end，如：23:45
     * @return 获取指定时间区间（前闭后闭）的值
     */
    @JsonIgnore
    public List<Double> getValueList(LocalTime start, LocalTime end) {
        return Arrays.asList(getValues()).subList((start.getHour() * 60 + start.getMinute()) / 15,
                (end.getHour() * 60 + end.getMinute()) / 15);
    }

}

